/**
  ******************************************************************************
  * @file    WF_ConnectSoftAP.c
  * @author  exatron
  * @version V1.0.0
  * @date    segunda, 22 de dezembro de 2014
  * @brief   Função para criar rede em modo AP.
  ******************************************************************************
  * @attention
  *
  *
  ******************************************************************************
  */

/** @addtogroup WF
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include <common.h>
#include <stdlib.h>

/* External functions --------------------------------------------------------*/

/** @defgroup WF_Functions
  * @{
  */

/**
  * @brief  Função para criar rede em modo AP.
  * @param  char *ssid: ssid da rede a criar
  * @param  char *key: key da rede a criar
  * @param  char *sectype: tipo de segurança da rede a criar
  * @retval Devolve pela serial Result_t
  */
void WF_ConnectSofAP(char *ssid, char *key, char *sectype)
{
	#ifndef DEBUG_ON_PC
	char cmd[255];
	#endif
	int i=0;

	char mode[50];
	char ip[50];
	char netmask[50];

	bzero(mode, sizeof(mode));
	bzero(ip, sizeof(ip));
	bzero(netmask, sizeof(netmask));

	strcpy(mode, aux_FileRdLine(PATH_CONF, "network", "Erro GetNetwork\r\n"));
	strcpy(ip, aux_FileRdLine(PATH_CONF, "ip", "Erro GetNetwork\r\n"));
	strcpy(netmask, aux_FileRdLine(PATH_CONF, "netmask", "Erro GetNetwork\r\n"));

	/* Verifica tamanho da chave */
	if(strlen(key) < 8)
	{
		aux_USART0_puts("%s=%d\r\n", "WF_CSAP", Result_BadArgument);
		aux_SetBusy(FALSE);
		return;
	}

	/* Verifica criptografia */
	if(strcmp(sectype, "psk") != 0)
	{
		if(strcmp(sectype, "psk2") != 0)
		{
			if(strcmp(sectype, "none") != 0)
			{
				aux_USART0_puts("%s=%d\r\n", "WF_CSAP", Result_BadArgument);
				aux_SetBusy(FALSE);
				return;
			}
		}
	}

    /* Verifica ip */
	else if(strcntc(ip,'.') < 3)
	{
    	aux_USART0_puts("%s=%d\r\n", "CFG_SNP", Result_BadArgument);
    	aux_SetBusy(FALSE);
    	return;
	}
    /* Verifica netmask */
	else if(strcntc(netmask,'.') < 3)
	{
    	aux_USART0_puts("%s=%d\r\n", "CFG_SNP", Result_BadArgument);
    	aux_SetBusy(FALSE);
    	return;
	}

	/* Fecha todos os sockets */
	for(i=0;i<(N_SOCK_TCP+N_SOCK_UDP);i++)
		aux_CloseSocket(i);

	aux_SetConnectionState(Status_ConnectionConnecting);

	#ifndef DEBUG_ON_PC
	/* Configure modo static network */
	if(atoi(mode) == STATIC_NETWOTK)
	{
		system("uci delete network.modo_sta > /dev/null 2> /dev/null");
		system("uci delete network.modo_ap > /dev/null 2> /dev/null");

		system("uci set network.modo_ap=interface");
		system("uci set network.modo_ap.proto=static");
		bzero(cmd,sizeof(cmd));
		sprintf(cmd,"%s%s","uci set network.modo_ap.ipaddr=",ip);
		system(cmd);
		bzero(cmd,sizeof(cmd));
		sprintf(cmd,"%s%s","uci set network.modo_ap.netmask=",netmask);
		system(cmd);
	}
	else
	{
		aux_USART0_puts("%s=%d\r\n", "WF_CSAP", Result_OnNetworkParameters);
		aux_SetBusy(FALSE);
		return;
	}

	/* Configure modo_ap wireless */
	system("uci delete wireless.modo_sta > /dev/null 2> /dev/null");
	system("uci delete wireless.modo_ap > /dev/null 2> /dev/null");
	system("uci set wireless.modo_ap=wifi-iface");
	system("uci set wireless.modo_ap.device=radio0");
	system("uci set wireless.modo_ap.mode=ap");
	system("uci set wireless.modo_ap.network=modo_ap");
	bzero(cmd,sizeof(cmd));
	sprintf(cmd,"%s%s%s","uci set wireless.modo_ap.ssid='",ssid,"'");
	system(cmd);
	bzero(cmd,sizeof(cmd));
	sprintf(cmd,"%s%s","uci set wireless.modo_ap.encryption=",sectype);
	system(cmd);
	bzero(cmd,sizeof(cmd));
	sprintf(cmd,"%s%s","uci set wireless.modo_ap.key=",key);
	system(cmd);

	system("uci delete dhcp.modo_sta > /dev/null 2> /dev/null");
	system("uci delete dhcp.modo_ap > /dev/null 2> /dev/null");
	system("uci set dhcp.modo_ap=dhcp");
	system("uci set dhcp.modo_ap.interface=modo_ap");
	system("uci set dhcp.modo_ap.start=50");
	system("uci set dhcp.modo_ap.limit=200");
	system("uci set dhcp.modo_ap.leasetime=1h");

	system("uci delete firewall.modo_sta_zone > /dev/null 2> /dev/null");
	system("uci delete firewall.modo_ap_zone > /dev/null 2> /dev/null");
	system("uci set firewall.modo_ap_zone=zone");
	system("uci set firewall.modo_ap_zone.name=modo_ap");
	system("uci set firewall.modo_ap_zone.network=modo_ap");
	system("uci set firewall.modo_ap_zone.input=ACCEPT");
	system("uci set firewall.modo_ap_zone.output=ACCEPT");
	system("uci set firewall.modo_ap_zone.forward=REJECT");

	system("uci delete firewall.modo_ap_rule_dhcp > /dev/null 2> /dev/null");
	system("uci set firewall.modo_ap_rule_dhcp=rule");
	system("uci set firewall.modo_ap_rule_dhcp.name='Allow DHCP request'");
	system("uci set firewall.modo_ap_rule_dhcp.src=modo_ap");
	system("uci set firewall.modo_ap_rule_dhcp.src_port=68");
	system("uci set firewall.modo_ap_rule_dhcp.dest_port=67");
	system("uci set firewall.modo_ap_rule_dhcp.proto=udp");
	system("uci set firewall.modo_ap_rule_dhcp.target=ACCEPT");
	system("uci set firewall.modo_ap_rule_dhcp.family=ipv4");

	system("uci commit");
	#endif

	aux_FileWrLine(PATH_CONF, "ssid", ssid, "Erro setando ssid\r\n");

	aux_SetConnectionState(Status_SoftAP);

	aux_USART0_puts("%s=%d\r\n", "WF_CSAP", Result_Ok);

	system("reboot -f > /dev/null 2> /dev/null");
}

/**
  * @}
  */

/**
  * @}
  */



